import {Component, OnInit} from '@angular/core';
import {PpypService} from '../../ppyp.service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {geti4CityNameById} from 'src/app/comm';

interface TABLE {
  query: any;
  listData: any;
}

interface USERMODAL {
  visible: boolean;
  data: any;
}

enum STATUS {
  '无效',
  '有效',
  '禁用'
}


@Component({
  selector: 'app-ppyp-user-manage',
  templateUrl: './ppyp-user-manage.component.html',
  styleUrls: ['./ppyp-user-manage.component.scss']
})
export class PpypUserManageComponent implements OnInit {
  STATUS = STATUS;
  table: TABLE = {
    query: {
      user: '',
      registerTime: [],
      page: 1,
      pageSize: 20
    },
    listData: {
      page: 1,
      pageSize: 20,
      total: 100,
      records: []
    }
  };
  userModal: USERMODAL = {
    visible: false,
    data: {}
  };
  addressArea: any;

  constructor(
    public ppypService: PpypService,
    private nzMessageService: NzMessageService
  ) {
  }

  ngOnInit(): void {
    import('../../../customization/manage/city').then(m => {
      this.addressArea = m.default;
    });
    this.getTableList();
  }

  onPageIndexChange(index: number) {
    this.table.query.page = index;
    this.getTableList();
  }

  onPageSizeChange(pageSize: number) {
    this.table.query.pageSize = pageSize;
    this.getTableList();
  }

  queryBtn() {
    this.table.query.page = 1;
    this.getTableList();
  }

  resetCondition() {
    this.table.query = {
      page: 1,
      pageSize: 20,
      user: '',
      registerTime: [],
    };
    this.queryBtn();
  }

  // 获取展示区域内容列表
  getTableList() {
    this.ppypService.userList({
      ...this.table.query,
      // @ts-ignore
      beginRegTime: this.ppypService.formatTime(this.table.query.registerTime ? this.table.query.registerTime[0] : '', 'date'),
      // @ts-ignore
      endRegTime: this.ppypService.formatTime(this.table.query.registerTime ? this.table.query.registerTime[1] : '', 'date'),
    }).then((res: any) => {
      if (res.code === 0) {
        this.table.listData = res.data;
      }
    });
  }

  toDetail(child: any) {

  }

  hideUserModal() {
    this.userModal.visible = false;
  }

  showUserModal(data: any) {
    this.userModal.visible = true;
    this.userModal.data = data;
  }

  SEX(v: number) {
    if (v === 1) {
      return '男';
    }
    if (v === 2) {
      return '女';
    }
    return '未知';
  }

  cityChangeName(id: any) {
    return geti4CityNameById(id)?.name || '未知';
  }
}
